<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <svg-icon
      :icon-class="isFullscreen ? 'exit-fullscreen' :'fullscreen'"
      class="fullscreen"
      @click="fullscreen"
    />
  </div>
</template>

<script>
import screenfull from 'screenfull'
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullscreen: false
    }
  },
  mounted() {
    // document.addEventListener('fullscreenchange', () => {
    //   // 通过! ! 可以将非布尔类型强制转换为布尔类型
    //   this.isFullscreen = !!document.fullscreenElement
    // })
    screenfull.on('change', () => {
      this.isFullscreen = screenfull.isFullscreen
    })
  },
  methods: {
    fullscreen() {
      if (this.isFullscreen) {
        screenfull.exit()
      } else {
        screenfull.request()
      }
      this.isFullscreen = !this.fullscreen
    }
    // fullscreen() {
    //   if (!this.isFullscreen) {
    //     // 全屏
    //     document.documentElement.requestFullscreen()
    //   } else {
    //     // 退出全屏
    //     document.exitFullscreen()
    //   }
    //   this.isFullscreen = !this.isFullscreen
    // }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper {
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
